package com.tech.view.component.button;

import com.tech.view.common.ColorUtil;
import com.tech.view.common.FontUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Rectangle2D;

/**
 * 灰色渐变登录按钮
 */
public class LoginButton extends JButton implements MouseListener {
	public static int DEFAULT_WIDTH = 250;
	public static int DEFAULT_HEIGHT = 36;
	float[] fractions = new float[]{0.0f, 0.4f, 0.6f, 1.0f};
	LinearGradientPaint gradientPaint;
	private int width;
	private int height;
	private String title;
	// private Color[] colors = new Color[]{
	// 		new Color(0x63a5f7).darker(),
	// 		new Color(0x3799f4).darker(),
	// 		new Color(0x2d7eeb).darker(),
	// 		new Color(0x30a5f9).darker()};
	private Color[] colors = new Color[]{
			new Color(84, 84, 87),
			new Color(60, 60, 68, 0),
			new Color(60, 60, 68).darker(),
			ColorUtil.BACKGROUND_COLOR.darker()
	};
	private Color[] colors2 = new Color[]{
			new Color(84, 84, 87).brighter(),
			new Color(60, 60, 68, 0),
			new Color(60, 60, 68),
			ColorUtil.BACKGROUND_COLOR.brighter()
	};
	private Color fontColor = ColorUtil.FONT_COLOR;
	private Font font;

	public LoginButton(String title) {
		this(DEFAULT_WIDTH, DEFAULT_HEIGHT, title);
	}

	public LoginButton(int width, int height, String title) {
		super(title);
		this.width = width;
		this.height = height;
		this.title = title;
		setPreferredSize(new Dimension(width, height));
		font = FontUtil.TITLE_FONT.deriveFont(height / 2f);
		gradientPaint = new LinearGradientPaint(0, 0, 0, height, fractions, colors);
		addMouseListener(this);
		setBorderPainted(false);
		setFocusPainted(false);
		setCursor(new Cursor(Cursor.HAND_CURSOR));
	}

	public void setTitle(String title) {
		this.title = title;
		repaint();
	}

	@Override
	public void mouseClicked(MouseEvent e) {
	}

	@Override
	public void mousePressed(MouseEvent e) {
		gradientPaint = new LinearGradientPaint(0, 0, 0, height, fractions, colors);
		repaint();
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		gradientPaint = new LinearGradientPaint(0, 0, 0, height, fractions, colors2);
		repaint();
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		gradientPaint = new LinearGradientPaint(0, 0, 0, height, fractions, colors2);
		repaint();
	}

	@Override
	public void mouseExited(MouseEvent e) {
		gradientPaint = new LinearGradientPaint(0, 0, 0, height, fractions, colors);
		repaint();
	}

	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		// 背景
		g2.setPaint(gradientPaint);
		g2.fillRoundRect(0, 0, width, height, 10, 10);
		// 字体
		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		g2.setFont(font);
		Rectangle2D bounds = font.getStringBounds(title, g2.getFontRenderContext());
		g2.setColor(fontColor);
		g2.drawString(title, (width - (int) bounds.getWidth()) / 2, (height + (int) bounds.getHeight() / 2) / 2);
	}

	@Override
	public void setText(String text) {
		this.title = text;
		repaint();
	}
}
